package com.tanhua.server.controller;

import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lk
 * @date 2022-03-24
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //获取id    Claims本质是map
        Long userId = UserHolder.getUserId();
        //向userinfo中设置用户id
        userInfo.setId(Long.valueOf(userId));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    @GetMapping
    public ResponseEntity users(Long userID, @RequestHeader("Authorization") String token) {
        //判断是否查询自己的数据
        if (userID == null) {   //userid为空则代表查询自己的数据
            //当前用户id
            Long userId = UserHolder.getUserId();
            userID = Long.valueOf(userId);
        }
        UserInfoVo userinfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userinfo);
    }

    //互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map<String, Integer> counts = userService.findCounts();
        return ResponseEntity.ok(counts);
    }

}
